package com.sjr.easy.chat.app.controller;

import com.alibaba.fastjson2.JSON;
import com.sjr.easy.chat.app.dto.AppTokenInfo;
import com.sjr.easy.chat.app.utils.AppTokenManager;
import com.sjr.easy.chat.business.ConversationBusiness;
import com.sjr.easy.chat.dto.Result;
import com.sjr.easy.chat.model.ChatGroupUser;
import com.sjr.easy.chat.model.Conversation;
import com.sjr.easy.chat.service.ChatGroupUserService;
import com.sjr.easy.chat.service.ContactService;
import com.sjr.easy.chat.service.ConversationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class AppConversationController {

    @Autowired
    private ConversationService conversationService;

    @Autowired
    private ContactService contactService;

    @Autowired
    private ChatGroupUserService chatGroupUserService;

    @Autowired
    private AppTokenManager appTokenManager;

    @Autowired
    private ConversationBusiness conversationBusiness;

    @Data
    static class GetOrCreateConversationReqParams {

        @ApiModelProperty("会话类型 1、一对一普通会话 2、群聊会话")
        private Integer type;

        @ApiModelProperty("联系人ID")
        private Long friendUserId;

        @ApiModelProperty("聊天群ID")
        private Long chatGroupId;
    }

    @ApiOperation("获取聊天会话，如果会话不存在，则创建")
    @PostMapping("/app/conversation/getOrCreateConversation")
    public Result<Conversation> getOrCreateConversation(@RequestBody GetOrCreateConversationReqParams reqParams) {
        log.info("reqParams:{}", JSON.toJSONString(reqParams));
        Objects.requireNonNull(reqParams.type, "会话类型不允许为空");

        AppTokenInfo tokenInfo = appTokenManager.getTokenInfo();
        Long selfUserId = tokenInfo.getId();
        Long friendUserId = reqParams.friendUserId;
        Conversation conversation = conversationBusiness.getOrCreateConversation(
                reqParams.type, selfUserId, friendUserId, reqParams.chatGroupId);
        log.info("conversation:{}", conversation);
        return Result.success(conversation);
    }

    @Data
    static class GetConversationReqParams {
        @ApiModelProperty("联系人ID")
        private Long friendUserId;

        @ApiModelProperty("聊天群ID")
        private Long chatGroupId;
    }

    @ApiOperation("获取聊天会话")
    @PostMapping("/app/conversation/getConversation")
    public Result<Conversation> getConversation(@RequestBody GetConversationReqParams reqParams) {
        log.info("reqParams:{}", JSON.toJSONString(reqParams));

        AppTokenInfo tokenInfo = appTokenManager.getTokenInfo();
        Long selfUserId = tokenInfo.getId();
        Long friendUserId = reqParams.friendUserId;
        Conversation conversation = conversationService.getConversation(selfUserId, friendUserId, reqParams.chatGroupId);
        log.info("conversation:{}", conversation);
        return Result.success(conversation);
    }


    @ApiOperation("批量查询聊天会话")
    @PostMapping("/app/conversation/getConversationByIdList")
    public Result<List<Conversation>> getConversationByIdList(@RequestBody List<Long> idList) {
        log.info("idList:{}", JSON.toJSONString(idList));
        AppTokenInfo tokenInfo = appTokenManager.getTokenInfo();
        Long selfUid = tokenInfo.getId();
        Set<Long> chatGroupIdSet = ListUtils.emptyIfNull(chatGroupUserService.selectByUserId(selfUid))
                .stream()
                .map(ChatGroupUser::getChatGroupId).collect(Collectors.toSet());

        List<Conversation> conversationList = conversationService.getByIdList(idList)
                .stream()
                .filter(item -> {
                    if (Objects.equals(item.getType(), 1)) {
                        return Objects.equals(item.getUid1(), selfUid) ||
                                Objects.equals(item.getUid2(), selfUid);
                    }
                    if (Objects.equals(item.getType(), 2)) {
                        return item.getChatGroupId() != null && chatGroupIdSet.contains(item.getChatGroupId());
                    }
                    return false;
                })
                .collect(Collectors.toList());
        log.info("conversationList:{}", JSON.toJSONString(conversationList));
        return Result.success(conversationList);
    }


}
